package com.yunzhong.appointment.system.service.impl;

import com.yunzhong.appointment.entity.Department;
import com.yunzhong.appointment.entity.Person;
import com.yunzhong.appointment.entity.Scheduling;
import com.yunzhong.appointment.mapper.DepartmentMapper;
import com.yunzhong.appointment.mapper.PersonMapper;
import com.yunzhong.appointment.mapper.SchedulingMapper;
import com.yunzhong.appointment.system.page.DoctorPage;
import com.yunzhong.appointment.system.service.DoctorService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private SchedulingMapper schedulingMapper;


    @Override
    public List<Person> queryByKsId(DoctorPage page, String id) {
        return personMapper.queryByKsId(id);
    }

    @Override
    public Department queryksById(String id,DoctorPage page) {
        return departmentMapper.queryksById(id);
    }

    @Override
    public Person queryDoctorById(String id) {
        return personMapper.queryDoctorById(id);
    }

    /**
     * 根据医生ID查询排班信息
     * @param id
     * @return
     */
    @Override
    public List<Scheduling> querySchedulingByDoctorId(String id) {
        return schedulingMapper.querySchedulingByDoctorId(id);
    }

    @Override
    public List<Person> listQdatdName(DoctorPage page) {
        return personMapper.listQdatdName(page);
    }

    @Override
    public List<Scheduling> selectSchedulingTime(String id, Integer year) {
        // new 个List
        List<Scheduling> list = new ArrayList<>();
        Date date = new Date();
        // 获取日
        int ri = date.getDate();
        // 获取月
        int yue = date.getMonth() + 1;
        // 获得当前月最后一天


        int maxri = getDayOfMonth();
        for (int i = 0; i < 9; i++) {
            // 拼年月日
            String sj = year + "-" + yue + "-" + ri;
            // 查询医生今天的排班
            Scheduling scheduling = schedulingMapper.queryScheduling(id, sj);

            if (i == 0) {
                if (scheduling != null) {
                    // 获取当前小时
                    int xs = date.getHours();
                    String time1 = scheduling.getTime1();
                    String time2 = scheduling.getTime2();
                    String time3 = scheduling.getTime3();
                    String time4 = scheduling.getTime4();
                    String time5 = scheduling.getTime5();
                    String time6 = scheduling.getTime6();
                    //截取前两位如果超过时间就把时间设置为null
                    if (StringUtils.isNotEmpty(time1)) {
                        time1 = time1.substring(0, 2);
                        Integer t1 = Integer.valueOf(time1);
                        if (xs >= t1) {
                            scheduling.setTime1(null);
                        }
                    }
                    if (StringUtils.isNotEmpty(time2)) {
                        time2 = time2.substring(0, 2);
                        Integer t2 = Integer.valueOf(time2);
                        if (xs >= t2) {
                            scheduling.setTime2(null);
                        }
                    }
                    if (StringUtils.isNotEmpty(time3)) {
                        time3 = time3.substring(0, 2);
                        Integer t3 = Integer.valueOf(time3);
                        if (xs >= t3) {
                            scheduling.setTime3(null);
                        }
                    }
                    if (StringUtils.isNotEmpty(time4)) {
                        time4 = time4.substring(0, 2);
                        Integer t4 = Integer.valueOf(time4);
                        if (xs >= t4) {
                            scheduling.setTime4(null);
                        }
                    }
                    if (StringUtils.isNotEmpty(time5)) {
                        time5 = time5.substring(0, 2);
                        Integer t5 = Integer.valueOf(time5);
                        if (xs >= t5) {
                            scheduling.setTime5(null);
                        }
                    }
                    if (StringUtils.isNotEmpty(time6)) {
                        time6 = time6.substring(0, 2);
                        Integer t6 = Integer.valueOf(time6);
                        if (xs >= t6) {
                            scheduling.setTime6(null);
                        }
                    }
                    if (scheduling != null) {
                        String standby = SetStandby(scheduling);
                        scheduling.setStandby(standby);
                    }
                    list.add(scheduling);
                } else {
                    list.add(scheduling);
                }

            } else {
                if (scheduling != null) {
                    //把拼好的日期放到实体里
                    String standby = SetStandby(scheduling);
                    scheduling.setStandby(standby);
                }
                list.add(scheduling);
            }
            // 如果当前天等于本月最大天数
            if (ri == maxri) {
                // 把日从1开始
                ri = 1;
                // 如果月等于12
                if (yue == 12) {
                    // 月从1开始
                    yue = 1;
                    // 年+1
                    year = year + 1;
                } else {
                    yue += 1;
                }
            } else {
                ri += 1;
            }
        }
        return list;

    }
    /**
     * 查询本月天数
     *
     * @return
     */
    public static int getDayOfMonth() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }
    /**
     * 查询出诊时间,不要重复,排序
     * @param scheduling
     * @return
     */
    @Override
    public Set<String> selectTime(List<Scheduling> scheduling) {
        Set<String> set = new TreeSet<>();
        for (int i = 0; i < scheduling.size(); i++) {
            // 遍历出诊时间
            Scheduling s = scheduling.get(i);
            if (s != null) {
                // 拿到每一个时间段的时间
                String t1 = s.getTime1();
                String t2 = s.getTime2();
                String t3 = s.getTime3();
                String t4 = s.getTime4();
                String t5 = s.getTime5();
                String t6 = s.getTime6();
                // 时间段不为空时 添加到set数组中
                if (t1 != null && !t1.equals("")) {
                    if (0 <= (Integer.parseInt(t1.substring(0, 2))) && (Integer.parseInt(t1.substring(0, 2))) < 12) {
                        set.add("上午" + t1);
                    } else {
                        set.add("下午" + t1);
                    }

                }
                if (t2 != null && !t2.equals("")) {
                    if (0 <= (Integer.parseInt(t2.substring(0, 2))) && (Integer.parseInt(t2.substring(0, 2))) < 12) {
                        set.add("上午" + t2);
                    } else {
                        set.add("下午" + t2);
                    }
                }
                if (t3 != null && !t3.equals("")) {
                    if (0 <= (Integer.parseInt(t3.substring(0, 2))) && (Integer.parseInt(t3.substring(0, 2))) < 12) {
                        set.add("上午" + t3);
                    } else {
                        set.add("下午" + t3);
                    }
                }
                if (t4 != null && !t4.equals("")) {
                    if (0 <= (Integer.parseInt(t4.substring(0, 2))) && (Integer.parseInt(t4.substring(0, 2))) < 12) {
                        set.add("上午" + t4);
                    } else {
                        set.add("下午" + t4);
                    }
                }
                if (t5 != null && !t5.equals("")) {
                    if (0 <= (Integer.parseInt(t5.substring(0, 2))) && (Integer.parseInt(t5.substring(0, 2))) < 12) {
                        set.add("上午" + t5);
                    } else {
                        set.add("下午" + t5);
                    }
                }
                if (t6 != null && !t6.equals("")) {
                    if (0 <= (Integer.parseInt(t6.substring(0, 2))) && (Integer.parseInt(t6.substring(0, 2))) < 12) {
                        set.add("上午" + t6);
                    } else {
                        set.add("下午" + t6);
                    }
                }
            }
        }
        // 移除空
        set.remove("");
        return set;
    }

    private String SetStandby(Scheduling scheduling) {
        Date schedulingDate = scheduling.getSchedulingDate();
        int day = schedulingDate.getDay();
        String xingqi = "";
        switch (day) {
            case 0:
                xingqi += "星期日";
                break;
            case 1:
                xingqi += "星期一";
                break;
            case 2:
                xingqi += "星期二";
                break;
            case 3:
                xingqi += "星期三";
                break;
            case 4:
                xingqi += "星期四";
                break;
            case 5:
                xingqi += "星期五";
                break;
            case 6:
                xingqi += "星期六";
                break;
            default:
                break;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        String str = formatter.format(schedulingDate);
        xingqi = str + xingqi;
        return xingqi;
    }

}
